<?php
/**
 *
 * @Author 夸克
 * @Email 79517721@qq.com
 * @Description //TODO
 * @Date
 * @Version 1.0
 * @WebSite www.dashubaba.com
 * @AllRight 大树巴巴科技开发团队
 *
 **/
include '../app/common.php';
include '../check.php';

$act = isset($_GET['act']) ? daddslashes($_GET['act']) : null;

@header('Content-Type: application/json; charset=UTF-8');


switch ($act) {


    /**
     * 获取大横幅广告数据
     */
    case 'getBigAdData':
        $where = "where 1 = 1";
        if (isset($_REQUEST['nickname']) && !empty($_REQUEST['nickname'])) {
            $param = trim(($_REQUEST['nickname']));
            $where = $where . " and nickname  like '%$param%'";
        }

        if (isset($_REQUEST['ad_qq']) && !empty($_REQUEST['ad_qq'])) {
            $param = trim(($_REQUEST['ad_title']));
            $where = $where . " and ad_qq like '%$param%'";
        }

        if (isset($_REQUEST['ad_url']) && !empty($_REQUEST['ad_url'])) {
            $param = trim(($_REQUEST['ad_url']));
            $where = $where . " and ad_url  like '%$param%'";
        }
        $bigAgList = $ADDB->query("SELECT t.*,curdate() as today FROM ad_big t $where  order by sort asc")->fetchAll();
        responseResult(0, '', $bigAgList, count($bigAgList));
        break;


    case 'editBigAd':
        $data = isQ();
        $nickname = $data['nickname'];
        $ad_qq = $data['ad_qq'];
        $ad_img = $data['ad_img'];
        $ad_url = $data['ad_url'];
        $start_time = $data['start_time'];
        $end_time = $data['end_time'];
        $sort = $data['sort'];
        $price = $data['price'];
        $default_img = $data['default_img'];
        $id = $data['id'];
        $sql = $ADDB->exec("UPDATE `ad_big` SET `nickname` ='{$nickname}',`ad_qq` ='{$ad_qq}',`ad_img` ='{$ad_img}',
                `ad_url` ='{$ad_url}',`start_time` ='{$start_time}',`end_time` ='{$end_time}',`sort` ='{$sort}',`price` ='{$price}'
                 ,`default_img` ='{$default_img}'WHERE `id`='{$id}'");
        responseResult(1);

        /*if ($sql) {
            responseResult(1);
        } else {
            responseResult(0,"修改失败");
        }*/

        break;

    case 'removeBigAd':
        try {
            $data = isQ($_REQUEST);
            $id = $data['id'];
            $sql = $ADDB->exec("DELETE FROM ad_big WHERE id='$id'");
        } catch (\Exception $e) {
            responseResult(0, "删除失败:" . $e);
        }
        responseResult(1);
        break;

    case 'batchRemoveBigAd':
        try {
            $data = isQ($_REQUEST);
            $arr = explode(",", $data['ids']);
            for ($i = 0; $i < count($arr); $i++) {
                $id = $arr[$i];
                $sql = $ADDB->exec("DELETE FROM ad_big WHERE id = {$id}");
            }
        } catch (\Exception $e) {
            responseResult(0, "删除失败:" . $e);
        }
        responseResult(1);
        break;


    case 'addBigAd':
        try {
            $data = isQ();
            $nickname = $data['nickname'];
            $ad_qq = $data['ad_qq'];
            $ad_img = $data['ad_img'];
            $ad_url = $data['ad_url'];
            $start_time = $data['start_time'];
            $end_time = $data['end_time'];
            $sort = $data['sort'];
            $price = $data['price'];
            $default_img = $data['default_img'];
            $id = $data['id'];
            $sql = $ADDB->exec("INSERT INTO ad_big   (`nickname`,  `ad_qq`,`ad_img`, `ad_url`, `start_time`, `end_time`
            , `sort`, `price`, `default_img`)VALUES
                    ('{$nickname}', '{$ad_qq}', '{$ad_img}',  '{$ad_url}', '{$start_time}','{$end_time}','{$sort}','{$price}'
                    ,'{$default_img}')");
        } catch (\Exception $e) {
            responseResult(0, "添加失败:" . $e);
        }
        responseResult(1);
        break;


    case 'changeBigAdIsShow':
        $data = isQ($_REQUEST);

        $id = $data['id'];
        $is_show = $data['is_show'];
        if (!in_array($is_show, [0, 1])) {
            responseResult(0, "参数错误");
        }

        $sql = $ADDB->exec("UPDATE `ad_big` SET `is_show` ='{$is_show}' WHERE `id`='{$id}'");
        responseResult(1);
        break;

    /**
     * 小横幅广告 start
     */

    case 'getSmallAdData':
        $where = "where 1 = 1";
        if (isset($_REQUEST['nickname']) && !empty($_REQUEST['nickname'])) {
            $param = trim(($_REQUEST['nickname']));
            $where = $where . " and nickname  like '%$param%'";
        }

        if (isset($_REQUEST['ad_title']) && !empty($_REQUEST['ad_title'])) {
            $param = trim(($_REQUEST['ad_title']));
            $where = $where . " and ad_title like '%$param%'";
        }

        if (isset($_REQUEST['ad_url']) && !empty($_REQUEST['ad_url'])) {
            $param = trim(($_REQUEST['ad_url']));
            $where = $where . " and ad_url  like '%$param%'";
        }
        $bigAgList = $ADDB->query("SELECT t.*,curdate() as today FROM ad_small t $where  order by sort asc")->fetchAll();
        responseResult(0, '', $bigAgList, count($bigAgList));
        break;

    case 'changeSmallAdIsShow':
        $data = isQ($_REQUEST);

        $id = $data['id'];
        $is_show = $data['is_show'];
        if (!in_array($is_show, [0, 1])) {
            responseResult(0, "参数错误");
        }

        $sql = $ADDB->exec("UPDATE `ad_small` SET `is_show` ='{$is_show}' WHERE `id`='{$id}'");
        responseResult(1);
        break;


    case 'editSmallAd':
        $data = isQ();
        $nickname = $data['nickname'];
        $ad_qq = $data['ad_qq'];
        $ad_img = $data['ad_img'];
        $ad_url = $data['ad_url'];
        $start_time = $data['start_time'];
        $end_time = $data['end_time'];
        $sort = $data['sort'];
        $price = $data['price'];
        $default_img = $data['default_img'];
        $id = $data['id'];

        $sql = $ADDB->exec("UPDATE `ad_small` SET `nickname` ='{$nickname}',`ad_qq` ='{$ad_qq}',`ad_img` ='{$ad_img}',
                `ad_url` ='{$ad_url}',`start_time` ='{$start_time}',`end_time` ='{$end_time}',`sort` ='{$sort}',`price` ='{$price}',
                `default_img` ='{$default_img}'  WHERE `id`='{$id}'");
        responseResult(1);

        /*if ($sql) {
            responseResult(1);
        } else {
            responseResult(0,"修改失败");
        }*/

        break;

    case 'removeSmallAd':
        try {
            $data = isQ($_REQUEST);
            $id = $data['id'];
            $sql = $ADDB->exec("DELETE FROM ad_small WHERE id='$id'");
        } catch (\Exception $e) {
            responseResult(0, "删除失败:" . $e);
        }
        responseResult(1);
        break;

    case 'batchRemoveSmallAd':
        try {
            $data = isQ($_REQUEST);
            $arr = explode(",", $data['ids']);
            for ($i = 0; $i < count($arr); $i++) {
                $id = $arr[$i];
                $sql = $ADDB->exec("DELETE FROM ad_small WHERE id = {$id}");
            }
        } catch (\Exception $e) {
            responseResult(0, "删除失败:" . $e);
        }
        responseResult(1);
        break;


    case 'addSmallAd':
        try {
            $data = isQ();
            $nickname = $data['nickname'];
            $ad_qq = $data['ad_qq'];
            $ad_img = $data['ad_img'];
            $ad_url = $data['ad_url'];
            $start_time = $data['start_time'];
            $end_time = $data['end_time'];
            $sort = $data['sort'];
            $price = $data['price'];
            $id = $data['id'];
            $default_img = $data['default_img'];
            $sql = $ADDB->exec("INSERT INTO ad_small   (`nickname`,  `ad_qq`,`ad_img`, `ad_url`, `start_time`, `end_time`
            , `sort`, `price`, `default_img`)VALUES
                    ('{$nickname}', '{$ad_qq}', '{$ad_img}',  '{$ad_url}', '{$start_time}','{$end_time}','{$sort}','{$price}'
                    ,'{$default_img}')");
        } catch (\Exception $e) {
            responseResult(0, "添加失败:" . $e);
        }
        responseResult(1);
        break;


    /**
     * 小横幅广告  end
     */


    /**
     * 文字广告 start
     */

    case 'getWordAdData':
        $where = "where 1 = 1";
        if (isset($_REQUEST['nickname']) && !empty($_REQUEST['nickname'])) {
            $param = trim(($_REQUEST['nickname']));
            $where = $where . " and nickname  like '%$param%'";
        }

        if (isset($_REQUEST['ad_title']) && !empty($_REQUEST['ad_title'])) {
            $param = trim(($_REQUEST['ad_title']));
            $where = $where . " and ad_title like '%$param%'";
        }

        if (isset($_REQUEST['ad_url']) && !empty($_REQUEST['ad_url'])) {
            $param = trim(($_REQUEST['ad_url']));
            $where = $where . " and ad_url  like '%$param%'";
        }
        $bigAgList = $ADDB->query("SELECT t.*,curdate() as today FROM ad_word t $where  order by sort asc")->fetchAll();
        responseResult(0, '', $bigAgList, count($bigAgList));

        break;


    case 'changeWordAdIsShow':
        $data = isQ($_REQUEST);

        $id = $data['id'];
        $is_show = $data['is_show'];
        if (!in_array($is_show, [0, 1])) {
            responseResult(0, "参数错误");
        }

        $sql = $ADDB->exec("UPDATE `ad_word` SET `is_show` ='{$is_show}' WHERE `id`='{$id}'");
        responseResult(1);
        break;



    case 'addWordAd':
        try {
            $data = isQ();
            $nickname = $data['nickname'];
            $ad_title = $data['ad_title'];
            $ad_url = $data['ad_url'];
            $start_time = $data['start_time'];
            $end_time = $data['end_time'];
            $sort = $data['sort'];
            $price = $data['price'];
            $ad_background = $data['ad_background'];
            $ad_text_color = $data['ad_text_color'];
            $id = $data['id'];
            $ad_qq = $data['ad_qq'];
            $default_title = $data['default_title'];


            $sql = $ADDB->exec("INSERT INTO ad_word   (`nickname`,  `ad_title`, `ad_url`, `start_time`, `end_time`
            , `sort`, `price`, `ad_background`, `ad_text_color`, `ad_qq`,`default_title`)VALUES
                    ('{$nickname}', '{$ad_title}',   '{$ad_url}', '{$start_time}','{$end_time}','{$sort}','{$price}'
                    ,'{$ad_background}','{$ad_text_color}','{$ad_qq}','{$default_title}' )");
        } catch (\Exception $e) {
            responseResult(0, "添加失败:" . $e);
        }
        responseResult(1);

        break;


    case 'editWordAd':
        $data = isQ();
        $nickname = $data['nickname'];
        $ad_title = $data['ad_title'];
        $ad_url = $data['ad_url'];
        $start_time = $data['start_time'];
        $end_time = $data['end_time'];
        $sort = $data['sort'];
        $price = $data['price'];
        $id = $data['id'];
        $ad_background = $data['ad_background'];
        $ad_text_color = $data['ad_text_color'];
        $ad_qq = $data['ad_qq'];
        $default_title = $data['default_title'];

        $sql = $ADDB->exec("UPDATE `ad_word` SET `nickname` ='{$nickname}',`ad_title` ='{$ad_title}',
                `ad_url` ='{$ad_url}',`start_time` ='{$start_time}',`end_time` ='{$end_time}',`sort` ='{$sort}',`price` ='{$price}'
                 ,`ad_background` ='{$ad_background}',`ad_text_color` ='{$ad_text_color}'
                  ,`ad_qq` ='{$ad_qq}',`default_title` ='{$default_title}' WHERE `id`='{$id}'");
        responseResult(1);

        break;

    case 'removeWordAd':
        try {
            $data = isQ($_REQUEST);
            $id = $data['id'];
            $sql = $ADDB->exec("DELETE FROM ad_word WHERE id='$id'");
        } catch (\Exception $e) {
            responseResult(0, "删除失败:" . $e);
        }
        responseResult(1);
        break;

    case 'batchRemoveWorAd':
        try {
            $data = isQ($_REQUEST);
            $arr = explode(",", $data['ids']);
            for ($i = 0; $i < count($arr); $i++) {
                $id = $arr[$i];
                $sql = $ADDB->exec("DELETE FROM ad_word WHERE id = {$id}");
            }
        } catch (\Exception $e) {
            responseResult(0, "删除失败:" . $e);
        }
        responseResult(1);
        break;


    /**
     * 文字广告  end
     */


    case 'saveEmailSet':
        $data = isQ();
        foreach ($data as $k => $value) {
            $value = daddslashes($value);
            $ADDB->query("insert into `ad_config` set `k`='{$k}',`v`='{$value}' on duplicate key update `v`='{$value}'");
        }
        responseResult(1);

        break;


    case 'savePaySet':
        $data = isQ();
        foreach ($data as $k => $value) {
            $value = daddslashes($value);
            $ADDB->query("insert into `ad_config` set `k`='{$k}',`v`='{$value}' on duplicate key update `v`='{$value}'");
        }
        responseResult(1);

        break;


    case 'saveSysSetting':
        $data = isQ();
        foreach ($data as $k => $value) {
            $value = daddslashes($value);
            $ADDB->query("insert into `ad_config` set `k`='{$k}',`v`='{$value}' on duplicate key update `v`='{$value}'");
        }
        responseResult(1);

        break;


    case 'saveUserInfo':
        $data = isQ();
        $username = $data['username'];
        if (isset($data['password']) && !empty($data['password'])) {
            $password = trim(strip_tags(daddslashes($data['password'])));
            if (strlen($password) < 6) {
                responseResult(0, "密码不能低于6位");
            }
            $password = md5($password);
            $sql = $ADDB->exec("UPDATE `ad_user` SET `username` ='{$username}',`password` ='{$password}' WHERE `id`='{$userrow['id']}'");
        } else {
            $sql = $ADDB->exec("UPDATE `ad_user` SET `username` ='{$username}' WHERE `id`='{$userrow['id']}'");

        }
        //正常修改
        if ($sql) {
            responseResult(1, "修改成功,请重新登录");
        } else {
            responseResult(0, "修改失败啦");
        }
        break;


    case 'getPayOrderData':
        $where = "where 1 = 1";
        if (isset($_REQUEST['nickname']) && !empty($_REQUEST['nickname'])) {
            $param = trim(($_REQUEST['nickname']));
            $where = $where . " and nickname  like '%$param%'";
        }

        if (isset($_REQUEST['ad_title']) && !empty($_REQUEST['ad_title'])) {
            $param = trim(($_REQUEST['ad_title']));
            $where = $where . " and ad_title like '%$param%'";
        }

        if (isset($_REQUEST['ad_url']) && !empty($_REQUEST['ad_url'])) {
            $param = trim(($_REQUEST['ad_url']));
            $where = $where . " and ad_url  like '%$param%'";
        }
        $bigAgList = $ADDB->query("SELECT t.* FROM ad_order t $where  order by date desc")->fetchAll();
        responseResult(0, '', $bigAgList, count($bigAgList));
        break;


    case 'removePayOrder':
        try {
            $data = isQ($_REQUEST);
            $id = $data['id'];
            $sql = $ADDB->exec("DELETE FROM ad_order WHERE id='$id'");
        } catch (\Exception $e) {
            responseResult(0, "删除失败:" . $e);
        }
        responseResult(1);

        break;


    case 'batchRemovePayOrder':
        try {
            $data = isQ($_REQUEST);
            $arr = explode(",", $data['ids']);
            for ($i = 0; $i < count($arr); $i++) {
                $id = $arr[$i];
                $sql = $ADDB->exec("DELETE FROM ad_order WHERE id = {$id}");
            }
        } catch (\Exception $e) {
            responseResult(0, "删除失败:" . $e);
        }
        responseResult(1);
        break;


    case 'getEchartsData':
        $thisYearList = $ADDB->query("SELECT t.* FROM ad_order t where status = 1 ")->fetchAll();
        $lastYearList = $ADDB->query("SELECT t.* FROM ad_order t where status = 1 ")->fetchAll();

        responseResult(1);

        break;

    default:

        break;


}

?>